/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sv;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author PC-PC
 */
@WebServlet(name = "AddSinhVien", urlPatterns = {"/AddSinhVien"})
public class AddSinhVien extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String username = null;
        String password = null;
        String firstname = null;
        String lastname = null;
        String email = null;


            username = request.getParameter("username");
            password = request.getParameter("passid");
            firstname = request.getParameter("firstname");
            lastname = request.getParameter("lastname");
            email = request.getParameter("email");
   

        
        //chen vao co so du lieu 
        if (insertUser(username, password, firstname, lastname, email)) {
            session.setAttribute("UserName", username);
            session.setAttribute("Access", "member");
            //gan sesssion cho UserId
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);

        } else {
            RequestDispatcher rd = request.getRequestDispatcher("registerMember.jsp");
            request.setAttribute("addMemberInvalid", false);
            rd.forward(request, response);
        }
    }
public static boolean insertUser(String username, String password, String firstname, String lastname, String email) {

        boolean isInsert = false;
        
        //ket noi voi co so du lieu
        Connection conn = getConnection();
        
        //chuyen password thanh ma md5
        String hashtest = convertMD5(password);
        
        //chuyen du lieu ve kieu ngay
        
        String call = "{call sp_Insert_Member(?,?,?,?,?,?,?,?,?)}";
        try {

            CallableStatement callbleStatememt = conn.prepareCall(call);
            callbleStatememt.setString(1, username);
            callbleStatememt.setString(2, password);
            callbleStatememt.setString(3, firstname);
            callbleStatememt.setString(4, lastname);
            callbleStatememt.setString(5, password);
            callbleStatememt.executeUpdate();
            isInsert = true;
            System.out.println("them thanh cong");
        } catch (SQLException e) {
            System.out.println("loi insert co so du lieu");
            e.printStackTrace();
        } finally {
            System.out.println("dong ket noi");
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return isInsert;

    }

public static String convertMD5(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte[] digit = md.digest();
            BigInteger bigInt = new BigInteger(1, digit);
            String hashtext = bigInt.toString(16);
            return hashtext;

        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException ex) {
            System.out.println(ex);
        }
        return null;
    }
 public static Connection getConnection() {
        Connection con = null;
        try {
            // ket noi vs Microsoft sql server
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://PC\\SQLEXPRESSt:1433;" + "databaseName=StudentManagement;";
            con = DriverManager.getConnection(url, "sa", "123456");
        } catch (ClassNotFoundException e) {            
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Loi Ket Noi!");
        }
        

        return con;

    }
}
